package cc.javaee.bbs.vo;

import cc.javaee.bbs.model.Huifu;
import cc.javaee.bbs.model.KbrPrizeCustomer;
import cc.javaee.bbs.model.activity.KbrActivityArrange;
import cc.javaee.bbs.model.activity.KbrActivityCustomer;
import cc.javaee.bbs.model.activity.KbrActivityPrize;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.springframework.format.annotation.DateTimeFormat;

import java.util.*;
import java.util.stream.Collector;
import java.util.stream.Collectors;

/**
 * @author xdw
 */
public class ActivityDetailsVo {

    public static final Integer MAX_PARTICIPATION_SIZE = 3;

    @Getter
    @Setter
    private Integer actId;                      //活动ID

    @Getter
    @Setter
    private String name;                        //活动名

    @Getter
    @Setter
    private String imgPath;                     //活动图片

    @Getter
    @Setter
    private String advImgPath;                  //活动图片

    @Getter
    @Setter
    private String describe;                    //活动介绍

    @Getter
    @Setter
    private Integer status;                     //活动状态 0尚未开始报名 1正在报名 2活动开始 3活动结束

    @Getter
    private Integer timeLeftSignUp;            //距离报名还有多少天

    @Getter
    private Integer timeLeftActivity;            //距离报名还有多少天

    @Getter
    @Setter
    private Integer mySignUpStatus;             //我的报名状态 1是已经报名 0是未报名

    @Getter
    @Setter
    private String type;                       //类型  用户上传文件/系统监测

    @Getter
    private List<Participation> participation;  //热门

    @Getter
    private List<ParticipationSYS> participationSYS; //热门系统监测

    @Getter
    private List<ActivityProcess> processList;  //活动流程

    @Setter
    @Getter
    private List<Discuss> reply;                //评论列表

    @Getter
    private List<Ranking> rankings;             //获取排名

    @Getter
    @Setter
    private List<KbrActivityPrize> prizes;

    @Getter
    @Setter
    private Integer walkCount;

    @Getter
    @Setter
    private Integer currentWalkCount;

    @Getter
    @Setter
    private Integer likeCount = 0;

    @Getter
    @Setter
    private Integer participationCount = 0;


    private Map<Integer, KbrActivityCustomer> applyMembers;  //报名人员

    private Map<String, KbrActivityCustomer> activityCustomer;

    public void setApplyMembers(Map<Integer, KbrActivityCustomer> applyMembers) {
        if (MapUtils.isNotEmpty(applyMembers)) {
            Map<String, KbrActivityCustomer> activityCustomer = applyMembers.values().stream()
                    .collect(Collectors.toMap(e -> e.getArchivalNumber(), e -> e));
            this.activityCustomer = activityCustomer;
        }
        this.applyMembers = applyMembers;
    }

    public void setTimeLeftSignUp(Date enStartTime) {
        Calendar cal = Calendar.getInstance();
        Calendar calEnd = Calendar.getInstance();
        calEnd.setTime(enStartTime);

        int i = cal.get(Calendar.DAY_OF_YEAR);
        int iEnd = calEnd.get(Calendar.DAY_OF_YEAR);

        this.timeLeftSignUp = iEnd - i;
    }

    public void setTimeLeftActivity(Date startTime) {
        Calendar cal = Calendar.getInstance();
        Calendar calEnd = Calendar.getInstance();
        calEnd.setTime(startTime);

        int i = cal.get(Calendar.DAY_OF_YEAR);
        int iEnd = calEnd.get(Calendar.DAY_OF_YEAR);

        this.timeLeftActivity = iEnd - i;
    }

    @Data
    public class ActivityProcess {
        private String name;
        private String startTime;
        private String emdTime;
    }

    public void process(List<KbrActivityArrange> arrangeList) {
        processList = new ArrayList<>();
        if (CollectionUtils.isEmpty(arrangeList)) {
            return;
        }
        arrangeList.forEach(e -> {
            ActivityProcess process = new ActivityProcess();
            process.setName(e.getName());
            if (e.getStartTime() != null) {
                process.setStartTime(String.format("%1$tm.%1$td", e.getStartTime()));
            }
            if (e.getEndTime() != null) {
                process.setEmdTime(String.format("%1$tm.%1$td", e.getEndTime()));
            }
            processList.add(process);
        });
    }

    @Data
    private class Ranking {
        private String imgPath;
        private String name;
        private String prizeName;
    }

    public String getImgPath(Integer custId) {
        String imgPath = "";
        if (applyMembers != null) {
            KbrActivityCustomer applyMember = applyMembers.get(custId);
            if (applyMember != null) {
                imgPath = applyMember.getImgPath();
            }
        }
        return imgPath;
    }

    public String getName(Integer custId) {
        String custName = "";
        if (applyMembers != null) {
            KbrActivityCustomer applyMember = applyMembers.get(custId);
            if (applyMember != null) {
                custName = applyMember.getCustName();
            }
        }
        return custName;
    }

    public void ranking(List<KbrPrizeCustomer> list) {
        rankings = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(list)) {
            list.forEach(e -> {
                Ranking ranking = new Ranking();
                ranking.setName(e.getCustName());
                ranking.setImgPath(getImgPath(e.getCustId()));
                ranking.setPrizeName(e.getPrizeName());
                rankings.add(ranking);
            });
        }
    }

    @Data
    class Discuss {
        private String imgPath;
        private String name;
        private String context;
        private Date createTime;
    }

    public void discuss(List<Huifu> huifus) {
        reply = new ArrayList<>();
        if (CollectionUtils.isEmpty(huifus)) {
            return;
        }
        huifus.forEach(e -> {
            Discuss discuss = new Discuss();
            discuss.setName(getName(e.getCreateuserid()));
            discuss.setImgPath(getImgPath(e.getCreateuserid()));
            discuss.setContext(e.getContenttxt());
            discuss.setCreateTime(e.getCreatetime());
            reply.add(discuss);
        });
    }

    @Data
    class Participation {
        private Integer likeCount;      //点赞数
        private String ImgPath;         //文件路径
        private Date createTime;
    }

    @Data
    class ParticipationSYS {
        private String name;
        private String imgPath;
        private String userImg;
        private Integer custId;
        private Integer walkCount;
    }

    public void participation(List<Huifu> list) {
        participation = new ArrayList<>();
        if (CollectionUtils.isEmpty(list)) {
            return;
        }
        for (int i = 0; i < list.size() && i < MAX_PARTICIPATION_SIZE; i++) {
            Huifu huifu = list.get(i);
            Participation p = new Participation();
            p.setImgPath(huifu.getImg());
            p.setLikeCount(huifu.getLikeCount());
            p.setCreateTime(huifu.getCreatetime());
            participation.add(p);
        }
    }

    public void participationSYS(Map<String, Integer> ranking) {
        participationSYS = new ArrayList<>();
        if (MapUtils.isEmpty(ranking)) {
            return;
        }

        ranking.forEach((k, v) -> {
            ParticipationSYS p = new ParticipationSYS();
            KbrActivityCustomer customer = activityCustomer.get(k);
            if (customer != null) {
                p.setCustId(customer.getCustId());
                p.setName(customer.getCustName());
                p.setImgPath(customer.getImgPath());
                p.setWalkCount(v);
                participationSYS.add(p);
            }
        });

        // 不足3个 补3个
        Collection<KbrActivityCustomer> customersCollection = applyMembers.values();
        if (participationSYS.size() < 3 && CollectionUtils.isNotEmpty(customersCollection)) {
            List<KbrActivityCustomer> customers = new ArrayList(customersCollection);
            for (int i = 0; i < 3 - participationSYS.size() && i < customers.size(); i++) {
                KbrActivityCustomer c = customers.get(i);
                ParticipationSYS p = new ParticipationSYS();
                p.setCustId(c.getCustId());
                p.setName(c.getCustName());
                p.setImgPath(c.getImgPath());
                p.setWalkCount(0);
                participationSYS.add(p);
            }
        }

    }

}
